Ext.BLANK_IMAGE_URL = 'resources/s.gif'; 
var Data_Me;
//存储获取的数据
var DataStore_Me;
//数据存储器
var Pager_Me;
//分页器
var PerCount_Me=100;
//每页的数据量
var Grid_Me;
//GridPanel容器
 var t_Me;
 var selectDataCompanyName_Me;
 var selectDataCompanyId_Me;
 var selectRowId;
 var addStatue=0;
function OnGetMemorandumDataSuccess(jsonText,row)//数据获取成功
{
	try{
   		Data_Me=Ext.util.JSON.decode(jsonText);
	}catch(e){
//		alert(e.name+":"+e.message);
	}
  //将获取的Json数据格式化
   DataStore_Me.loadData(GetPager_MeData(Data_Me),false);
   DataStore_Me.load();
  //这个很重要
   Pager_Me.doLoad(0);
  
}

function GetPager_MeData(InData)//从静态数据中获取每页的数据
{
   var TempData_Me=//为什么要用'd'作为json数据的根，因为从WebService获取Json数据经查看就是以'd'为根的
  {
    'd':[]
  };
   if(InData.d.length>PerCount_Me)
  {
     for(var i=0;i<=PerCount_Me-1;i++)
    {
       TempData_Me.d.push(InData.d);
    }
  }
   else
  {
     for(var i=0;i<=InData.d.length-1;i++)
    {
       TempData_Me.d.push(InData.d);
    }
  }
   return TempData_Me;
}
function SerGrid_Me()
{
	//获取数据之前的Loading数据
   Data_Me=
  {
    'd':[
    {
      'id':'数据载入中',
      'companyId':'数据载入中',
      'companyName':'数据载入中',
      'memorandum':'数据载入中',
      'updateTime':'数据载入中'
    }
    ]
  };
   DataStore_Me=new Ext.data.Store(
  {
    //数据存储器
     proxy:new Ext.data.MemoryProxy(GetPager_MeData(Data_Me)),
     reader:new Ext.data.JsonReader(
    {
       root:'d'
    },
    [
    {
       name:'id'
    },
    {
       name:'companyId'
    },
    {
       name:'companyName'
    },
    {
       name:'memorandum'
    },
    {
       name:'updateTime'
    }
    
    ])
  });
   var ColM=new Ext.grid.ColumnModel(//行定义器
  [
  {
     header:"跟踪情况",dataIndex:"memorandum",sortable :true,width:256
  },  
  {
     header:"日期",dataIndex:"updateTime",sortable :true,width:120
  }
  ]);
   Pager_Me=new Ext.PagingToolbar(
  {
     id:'Grid_Pager_Me',
     pageSize:PerCount_Me,
     store:DataStore_Me,
     displayInfo:true,
     paramNames:
    {
       start:'start',limit:'limit',cid:'cid'
    },
     displayInfo:true,
     displayMsg:'显示第 {0} 条到 {1} 条记录，一共 {2} 条',
     emptyMsg:'没有数据',
     updateInfo:function ()//重写UpdateInfo
    {
       if(this.displayEl)
      {
         var count=this.store.getCount();
         var msg=count==0?this.emptyMsg:String.format(this.displayMsg,this.cursor+1,Math.min(this.cursor+this.pageSize-1,Data_Me.d.length-1)+1,Data_Me.d.length);
         this.displayEl.update(msg);
      }
    },
     onLoad:function (store,r,o)//重写OnLoad
    {
       if(!this.rendered)
      {
         this.dsLoaded=[store,r,o];
         return ;
      }
       var d=this.getPageData(),ap=d.activePage,ps=d.pages;
       this.afterTextEl.el.innerHTML=String.format(this.afterPageText,d.pages);
       this.field.dom.value=ap;
       this.first.setDisabled(ap==1);
       this.prev.setDisabled(ap==1);
       this.next.setDisabled(ap==ps);
       this.last.setDisabled(ap==ps);
       this.loading.enable();
       this.loading.setVisible(false);
       this.updateInfo();
    },
     doLoad:function (start)//重写doLoad
    {
    	if(start==0){
    		this.cursor=0;
    	}
       var TempData_Me=
      {
        'd':[]
      };
       var i=0;
       var len=this.pageSize;
       for(i=0;i<len;i++)
      {
         if(Data_Me.d[start+i]!=null)
        {
           TempData_Me.d.push(Data_Me.d[start+i]);
        }
      }
       this.store.loadData(TempData_Me,false);
    },
    readPage : function(d){
     	this.cursor=this.pageSize*(this.field.dom.value-1);
     	 t=this.cursor;
       	this.doLoad(t);
    },
     getPageData:function ()//重写getPageData
    {
       var A=Data_Me.d.length;
       return{
			total:A,
			activePage:Math.ceil((this.cursor+this.pageSize)/this.pageSize),
			pages:A<this.pageSize?1:Math.ceil(A/this.pageSize)}},
     onClick:function (which)//重写onClick
    {
       switch(which)
      {
      	
         case "first":
         t=0;
         this.cursor=Math.max(0,t-this.pageSize);
         this.doLoad(0);
         break;
         case "prev":
         t_Me=this.cursor;
         this.cursor=Math.max(0,t_Me-this.pageSize);
         this.doLoad(Math.max(0,t_Me-this.pageSize));
         break;
         case "next":
         t_Me=this.cursor;
         this.cursor=t_Me+this.pageSize;
         this.doLoad(t_Me+this.pageSize);
         break;
         case "last":
         var total=Data_Me.d.length;
         var extra=total%this.pageSize;
         var lastStart=extra?(total-extra):total-this.pageSize;
         this.cursor=extra?(total-extra):total-this.pageSize;
         this.doLoad(lastStart);
         break;
         case "refresh":
         t_Me=this.cursor;
         this.cursor=t_Me;
         this.doLoad(t_Me);
         break;
      }
    }
  });
  
  
  
    gridForm_Me = new Ext.FormPanel({
        id: 'memorandum-form',
        frame: true,
        labelAlign: 'left',
        title: '备忘录',
        bodyStyle:'padding:5px',
        width: 400,
        height: 500,
        layout: 'column',	
        items: [{
            columnWidth: 1,
            layout: 'fit',
            id:'item1',
            items: {
           		id:'panelGrid_Me',
	            xtype: 'editorgrid',
	            store:DataStore_Me,
	            cm:ColM,
	            sm: new Ext.grid.RowSelectionModel({
	                singleSelect: true,
	                listeners: {
	                    rowselect: function(sm, row, rec) {
	                    	selectDataCompanyName_Me = rec.data.companyName;
	                    	selectDataCompanyId_Me=rec.data.id;
	                    	selectRowId=row;
	                        Ext.getCmp("memorandum-form").getForm().loadRecord(rec);
	                        if(rec.data.id==""){
	                        	Ext.getCmp("submitCon").setVisible(false);
	                        }else{
	                        	Ext.getCmp("submitCon").setVisible(true);
	                        }
	                        Ext.getCmp("submitCon").setText("保 存"); 
	                        addStatue=0;
	                        if(Ext.getCmp("memorandum_Value").getValue()!=""){
	                        	Ext.getCmp("memorandum_Value").getEl().dom.readOnly=false;
	                        }
	                    }
	                }
	            }),
	            tbar:[{
		            text:'新增',
		            tooltip:'新增',
		            id:'addNew',
		            handler:function (){
		            		try{ 
		            			if(selectDataCompanyName==""){
		            				alert("请选择名录列表中需要备忘的数据");
		            				return;
		            			}
		            			Ext.getCmp("submitCon").setText("保存"); 
		            			if(addStatue==0){
		            				Ext.getCmp("memorandum_Value").getEl().dom.readOnly=false;  
		            				Ext.getCmp("memorandum_Value").setValue("");          			
		            				Ext.getCmp("submitCon").setVisible(true);
		            			}else{
		           					Ext.getCmp("submitCon").setVisible(false);
		            				Ext.getCmp("memorandum_Value").getEl().dom.readOnly=true;
			            		}
		            		}catch(e){
//		            			alert(e.message);
		            		}
		            	} ,
		            iconCls:'addData'
			        }, '-', {
			            text:'删除',
			            tooltip:'删除',
			            id:'delete',
			            handler:function (){
			            	try{
			            		if(selectDataCompanyId_Me==""){
			            			alert("请选择需要删除的数据");
		            				return;
			            		}
								DataStore_Me.remove(DataStore_Me.getAt(selectRowId));
								Data.d.splice(Pager_Me.cursor+selectRowId,1);
								Ext.getCmp("memorandum_Value").setValue("");
								sendNSCommand('deleteMemorandum'+selectDataCompanyId_Me);
								selectDataCompanyId_Me="";
	                        }catch(e){
//	                        	alert(e.message);
	                        }
			            },
			            iconCls:'delete'
			        }
			        ],
	            height: 310,
	            border: true,
		        listeners: {
		        	render: function(g) {
		        		g.getSelectionModel().selectRow(0);
		        	},
		        	delay: 10 // Allow rows to be rendered.
		        },
		        bbar:Pager_Me
        	}
        },{
        	columnWidth: 1,
            xtype: 'panel',
            autoHeight: true,
            items: [
                        
           {   layout : 'table',
               layoutConfig : {
                 columns : 4 //设置表格布局默认列数为4列
               },	           
			 	items:[		
			    {
			     title:'',
			     colspan : 4,   //设置跨列
				 defaultType: 'textfield', layout : 'form',
				  items: [
						 {fieldLabel: '备忘内容',xtype: 'textarea',id:"memorandum_Value",name: 'memorandum',readOnly:true,anchor: '100% 0',height:'120',width:'340'
			            } ]
			    },	
			    {
			     title:'',
			     colspan : 1 ,  //设置跨列
				 width:15
			    }
			    
			    
			    ]

					}
				]
			
        }],
			     buttons:[      
                             {
                             id:"submitCon",
                             text:"保存",   
                              buttonAlign:'center', 
                              width:'150',
                              hidden:true,
                              visible:false,                           
                              handler:function(){
                              		if(Ext.getCmp("memorandum_Value").getValue()==""){
					            		alert("请填写内容");
					            		return;
					            	}
					            	if(Ext.getCmp("submitCon").getText()=="保存"){
					            	addStatue=0;
					            	Ext.getCmp("submitCon").setVisible(false);     
                                    sendNSCommand('addMemorandum'+";"+selectDataCompanyId+";"+selectDataCompanyName+";"+Ext.getCmp("memorandum_Value").getValue());
					            	}else{
					            		try{
					            			if(selectDataCompanyId_Me==""){
					            				alert("请选择需要修改的数据");
				            					return;
					            			}
											sendNSCommand('updateMemorandum'+';'+selectDataCompanyId_Me+";"+Ext.getCmp("memorandum_Value").getValue()+";"+selectDataCompanyId+";"+selectDataCompanyName);
				                        }catch(e){
//				                        	alert(e.message);
		                        }
					            	}  
                              }}      
                         ] 
    });



//   DataStore_Me.loadData(GetPager_MeData(Data),false);
   DataStore_Me.load();
//   Pager_Me.doLoad(0);
  //这个很重要，用来显示Loading数据
}

function Main_Me()
{
	SerGrid_Me();
   gridForm_Me.render(document.getElementById("jqbody-menu2"));
 
}







Ext.onReady(function(){
	Main_Me();
	
});





function GetRequest_Me() {

   var url = location.search; //获取url中"?"符后的字串

   var theRequest = new Object();

   if (url.indexOf("?") != -1) {

      var str = url.substr(1);

      strs = str.split("&");

      for(var i = 0; i < strs.length; i ++) {

         theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);

      }

   }

   return theRequest;

}
